//
//  SKUDetailTbv_V4_Buy_Cell.m
//  ZITHER
//
//  Created by weixin on 2018/11/10.
//  Copyright © 2018 weixn. All rights reserved.
//

#import "SKUDetailTbv_V4_Buy_Cell.h"
//#import "SKUDetailNewVc.h"
#import "STViewController.h"

@implementation SKUDetailTbv_V4_Buy_Cell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
//    self.backgroundColor = [UIColor redColor];
    [self setUI];
    [self setMasonry];
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] init];
    self.userInteractionEnabled = YES;
    [self addGestureRecognizer:tap];
    
    
    
        [RACObserve(self,tbvTitle.contentSize)subscribeNext:^(id  _Nullable x) {
            NSLog(@"self.height = %f",self.height);
            if (self.tbvTitle.contentSize.height>self.height) {
                self.height = self.tbvTitle.contentSize.height;
            }
        }];

    
    @weakify(self);
    [[tap rac_gestureSignal]subscribeNext:^(__kindof UIGestureRecognizer * _Nullable x) {
        @strongify(self);
        self.btn.selected = !self.btn.selected;
        [self chageBtn];
        
        if (self.tbvTitle.height == 200) {
//            [self.tbvTitle reloadData];
            CGFloat f= self.tbvTitle.contentSize.height;
            self.tbvTitle.contentOffset = CGPointMake(0, 1);
            [self.tbvTitle mas_updateConstraints:^(MASConstraintMaker *make) {
                make.height.mas_equalTo(self.height+20);
            }];
            if (self.blcok) {
                self.blcok();
            }
        }else{
            [self.tbvTitle mas_updateConstraints:^(MASConstraintMaker *make) {
                make.height.mas_equalTo(200);
            }];
        
            if (self.blcok) {
                self.blcok();
            }
        }
//        [self setNeedsLayout];
//        [self layoutIfNeeded];

    }];

   
//    [RACObserve(self,tbvTitle.contentSize)subscribeNext:^(id  _Nullable x) {
//
//        CGFloat f= self.tbvTitle.contentSize.height;
//        self.tbvTitle.frame = CGRectMake(kScreenW+10,10 , kScreenW-20, f);
//        [self.tbvTitle mas_updateConstraints:^(MASConstraintMaker *make) {
//            make.height.mas_equalTo(f+20);
//        }];
//        if (self.blcok) {
//            self.blcok();
//        }
//    }];

}

-(CGFloat)measureHeightOfUITextView:(UITextView *)textView
{
    if ([textView respondsToSelector:@selector(snapshotViewAfterScreenUpdates:)])
    {
        // This is the code for iOS 7. contentSize no longer returns the correct value, so
        // we have to calculate it.
        //
        // This is partly borrowed from HPGrowingTextView, but I've replaced the
        // magic fudge factors with the calculated values (having worked out where
        // they came from)
        
        CGRect frame = textView.bounds;
        
        // Take account of the padding added around the text.
        
        UIEdgeInsets textContainerInsets = textView.textContainerInset;
        UIEdgeInsets contentInsets = textView.contentInset;
        
        CGFloat leftRightPadding = textContainerInsets.left + textContainerInsets.right + textView.textContainer.lineFragmentPadding * 2 + contentInsets.left + contentInsets.right;
        CGFloat topBottomPadding = textContainerInsets.top + textContainerInsets.bottom + contentInsets.top + contentInsets.bottom;
        
        frame.size.width -= leftRightPadding;
        frame.size.height -= topBottomPadding;
        
        NSString *textToMeasure = textView.text;
        if ([textToMeasure hasSuffix:@"\n"])
        {
            textToMeasure = [NSString stringWithFormat:@"%@-", textView.text];
        }
        
        NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
        [paragraphStyle setLineBreakMode:NSLineBreakByWordWrapping];
        
        NSDictionary *attributes = @{ NSFontAttributeName: textView.font, NSParagraphStyleAttributeName : paragraphStyle };
        
        CGRect size = [textToMeasure boundingRectWithSize:CGSizeMake(CGRectGetWidth(frame), MAXFLOAT)
                                                  options:NSStringDrawingUsesLineFragmentOrigin
                                               attributes:attributes
                                                  context:nil];
        
        CGFloat measuredHeight = ceilf(CGRectGetHeight(size) + topBottomPadding);
        return measuredHeight+8.0f;
    }
    else
    {
        return textView.contentSize.height;
    }
}



-(BaseBtn *)btn{
    if (!_btn) {
        _btn = [BaseBtn initCustomBtnImageAndTitles:@[@"全部",@"收起"] images:@[@"SKU_下拉",@"SKU_收起"] color:@[baseColorTheme,baseColorTheme]];
//        _btn.backgroundColor = [UIColor redColor];
        _btn.titleLabel.font = [UIFont fontWithName:@"PingFangSC-Medium" size:12];
        _btn.enabled = NO;
        _btn.imageView.contentMode = UIViewContentModeScaleAspectFit;
        [_btn sizeToFit];
        _btn.titleEdgeInsets = UIEdgeInsetsMake(0, 0, 0, -30);
        _btn.imageEdgeInsets = UIEdgeInsetsMake(0, 0, 0, -50);
        
    }
    return _btn;
}


-(void)chageBtn{
    
    if (self.btn.selected) {
        [self.btn setTitle:@"收起" forState:UIControlStateNormal];
        [self.btn setTitleColor:baseColorTheme forState:UIControlStateNormal];
        [self.btn setImage:[UIImage imageNamed:@"SKU_收起"] forState:UIControlStateNormal];
    }else{
        [self.btn setTitle:@"全部" forState:UIControlStateNormal];
        [self.btn setTitleColor:baseColorTheme forState:UIControlStateNormal];
        [self.btn setImage:[UIImage imageNamed:@"SKU_下拉"] forState:UIControlStateNormal];
    }
}

- (BaseImageV *)imageV{
    if (!_imageV) {
        _imageV = [[BaseImageV alloc]initializeFrame:CGRectMake(0, 0, 0, 0) contentMode:2];
        _imageV.image = [UIImage imageNamed:@"sku_半透明遮罩"];
        _imageV.backgroundColor = [UIColor clearColor];
    }
    return _imageV;
}



-(void)setUI{
    
//    [self addSubview:self.bizBuyLab];
//    [self addSubview:self.userBuyLab];
//    [self buyBtn];
    
    [self addSubview:self.tbvTitle];
    [self addSubview:self.imageV];
    [self addSubview:self.btn];
 }

-(void)setModel:(skuDetailModel *)model
{
    _model = model;
    self.tbvTitle.model = model;

//    self.bizBuyLab.text = [NSString stringWithFormat:@"用户求购 %@次",model.enquiryCount];
//    self.userBuyLab.text = [NSString stringWithFormat:@"商家报价 %@次",model.offerCount];

}

-(MainDetailTopPropertyTbv *)tbvTitle{
    if (!_tbvTitle) {
        _tbvTitle = [[MainDetailTopPropertyTbv alloc]initWithFrame:CGRectMake(kScreenW+10,0 , kScreenW-20,SCREEN_WIDTH-30)];
        _tbvTitle.backgroundColor = [UIColor whiteColor];
        _tbvTitle.scrollEnabled = NO;

    }
    return  _tbvTitle;
}


-(void)setMasonry{
    [self.tbvTitle mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.equalTo(self).offset(0);
        make.height.mas_equalTo(200);
        make.bottom.equalTo(self).offset(-10);
    }];
    
    
    [self.imageV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.equalTo(self).offset(0);
        make.height.mas_equalTo(40);
    }];
    
    
    [self.btn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self).offset(15);
        make.right.equalTo(self).offset(-15);
        make.width.mas_equalTo(60);
        make.height.mas_equalTo(20);
    }];
    [self addLineView];
//    [self.bizBuyLab mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.top.equalTo(self.mas_top).offset(0);
//        make.right.equalTo(self.mas_centerX).offset(-5);
//    }];
//
//    [self.userBuyLab mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.top.equalTo(self.mas_top).offset(0);
//        make.left.equalTo(self.mas_centerX).offset(5);
//    }];
//
//    [self.buyBtn mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.top.equalTo(self.bizBuyLab.mas_bottom).offset(12);
//        make.left.equalTo(self).offset(15);
//        make.right.equalTo(self).offset(-15);
//        make.height.mas_equalTo(45);
//        make.bottom.equalTo(self).offset(-30);
//    }];
//
//
//    [self.buyBtn mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.top.equalTo(self.bizBuyLab.mas_bottom).offset(12);
//        make.left.equalTo(self).offset(15);
//        make.right.equalTo(self).offset(-15);
//        make.height.mas_equalTo(45);
//        make.bottom.equalTo(self).offset(-30);
//    }];

}
-(void)addLineView{
    BaseView *view = [[BaseView alloc]init];
    view.backgroundColor = HexColor(0xf5f5f5);
    [self addSubview:view];
    [view mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.mas_bottom).offset(0);
        make.left.right.equalTo(self).offset(0);
        make.height.mas_equalTo(10);
        
    }];
}

//-(BaseImageV *)addImage{
//
//    BaseImageV *imageV = [[BaseImageV alloc]initializeFrame:CGRectMake(kScreenW/2.0-100, kScreenH-TabbarHeight-55-20-20, 200,55 ) contentMode:1];
//    imageV.image = [UIImage imageNamed:@"功能引导页_询价"];
//    imageV.backgroundColor = [UIColor clearColor];
//    [self addSubview:imageV];
//    [[imageV addTag]subscribeNext:^(id  _Nullable x) {
//        imageV.hidden = YES;
//        //        imageV = nil;
//    }];
//
//    [imageV mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.bottom.equalTo(self.userBuyLab.mas_bottom).offset(25);
//        make.centerX.equalTo(self.mas_centerX).offset(0);
//        make.width.mas_equalTo(237/4*3);
//        make.height.mas_equalTo(65/4*3);
//    }];
//
//    UserInfoManager *model = kUSER_INFO_MANAGER;
//    model.isSTViewController = YES;
//    [kUSER_INFO_MANAGER setUserInfoWithModel:model];
//   return imageV;
//}
//
//
//
//-(BaseLab *)bizBuyLab{
//    if (!_bizBuyLab) {
//        _bizBuyLab = [[BaseLab alloc]initializeFrame:CGRectMake(15, 486, 28, 14) text:@"用户求购 76次" font:10 textColor:[UIColor blackColor] isBold:NO numberOfLines:1];
//        _bizBuyLab.font = [UIFont fontWithName:@"PingFangSC-Medium" size:14];
//        _bizBuyLab.hidden = YES;
//    }
//    return _bizBuyLab;
//}
//
//
//-(BaseLab *)userBuyLab{
//    if (!_userBuyLab) {
//        _userBuyLab = [[BaseLab alloc]initializeFrame:CGRectMake(15, 486, 28, 14) text:@"商家报价181次" font:10 textColor:[UIColor blackColor] isBold:NO numberOfLines:1];
//        _userBuyLab.font = [UIFont fontWithName:@"PingFangSC-Medium" size:14];
//        _userBuyLab.hidden = YES;
//    }
//    return _userBuyLab;
//}
//
//- (BaseBtn *)buyBtn{
//    if (!_buyBtn) {
//        _buyBtn = [[BaseBtn alloc]initializeFrame:CGRectMake(0, 0, 1, 1) NormalText:@"发布求购" NormalTextColor:[UIColor whiteColor] SeleText:@"重启询价" SeleTextColor:[UIColor whiteColor] font:10 isBold:NO];
//        _buyBtn.hidden = YES;
//        [self addSubview:_buyBtn];
//
//        UIImageView *image = nil;
//        if (!kUSER_INFO_MANAGER.isSTViewController) {
//            image = [self addImage];
//        }
//
//
////        _buyBtn.backgroundColor = [UIColor randomColor];
//        _buyBtn.backgroundColor = [UIColor RGBColorWithR:123 g:123 b:237];
//        _buyBtn.titleLabel.font = [UIFont fontWithName:@"PingFangSC-Medium" size:17];
//
//
//        [_buyBtn setCornerRadiusWithRadius:8 isMasks:YES];
//        @weakify(self);
//        [[_buyBtn rac_signalForControlEvents:(UIControlEventTouchUpInside)] subscribeNext:^(__kindof UIControl * _Nullable x) {
//            @strongify(self);
//            if (!kUSER_INFO_MANAGER.userIsLogin) {
//                [Tools pushSinge];
//                return ;
//            }
//            [[serviceDefualt loadopenEnquiry:self.model.goodsId]subscribeNext:^(ResponseData* x) {
//                if (x.success) {
//                    [kUSER_INFO_MANAGER MyList];
//
//                    if (image != nil) {
//                        image.hidden = YES;
//                    }
//
//                    if (self.buyBtn.selected) {
//                        [Tools showTips:@"重启询价成功"];
//                        if (self.OpenQuestion) {
//                            self.OpenQuestion();
//                        }
//
//                    }else{
//                        self.buyBtn.selected = !self.buyBtn.selected;
//                        [Tools showTips:@"开启询价成功"];
//                        if (self.OpenQuestion) {
//                            self.OpenQuestion();
//                        }
//
//                    }
//
//                }else{
//                    [Tools showTips:x.errorInfo];
//                }
//            }];
//        }];
//    }
//    return _buyBtn;
//}


@end
